// pages/loupan/detail.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:'',
        detail: '',
        moretext: "点击查看更多",
        showmore: false,
        liked: false,
        imglist: '',
        wuyetype: '', // 物业类型
        wenda: [],
        modalName:'',
        picker: [
            '男', '女'
        ],
        value: '男',
        active:0,
        houseid:'',
        name:'',
        tel:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        console.log(options.id);
        var that = this;
        that.setData({
            id: options.id
        })
        that.getdetail()
    },
    getdetail(){
        wx.showLoading({
            title: '加载中',
        })
        var that = this;
        var id =that.data.id;
        app.core('get','get_sale_online_detail/id/'+id+'','',(res=>{
            console.log(res,'详情')
            that.setData({
                detail:res.data, house:res.house
            })
            wx.hideLoading()
        }))
    },


    // detail() {
    //   var id = this.data.id;
    //   var that = this;
    //   var opid = wx.getStorageSync('user').openid;
    //   app.core('get', 'get_houses_detail/id/' + id + '/open_id/' + opid + '', {}
    //     , (res) => {
    //       console.log(res)

    //       var newres = res.data;
    //       var wuyetype = res.data.propertyType;
    //       switch (wuyetype) {
    //         case "-1-3-":
    //           wuyetype = "-住宅-写字楼-"
    //           break;
    //         case "-1-2-":
    //           wuyetype = "-住宅-商铺-"
    //           break;
    //         case "-1-2-3":
    //           wuyetype = "-住宅-商铺-写字楼-"
    //           break;
    //         case "-2-3-":
    //           wuyetype = "-商铺-写字楼-"
    //           break;
    //       }


    //       var arr = [];
    //       if (res.data.buildNoPhoto) {
    //         arr.push(res.data.buildNoPhoto);
    //       }
    //       if (res.data.effectPhoto) {
    //         arr.push(res.data.effectPhoto);
    //       }
    //       if (res.data.trafficPhoto) {
    //         arr.push(res.data.trafficPhoto);
    //       }
    //       if (res.data.dimensionPhoto) {
    //         arr.push(res.data.dimensionPhoto);
    //       }
    //       if (res.data.videoPhoto) {
    //         arr.push(res.data.videoPhoto);
    //       }
    //       that.setData({
    //         wuyetype: wuyetype,
    //         detail: res.data,
    //         liked: res.hasCollect
    //       })
    //       that.setData({
    //         imglist: arr
    //       })
    //       var x = res.data.loupan_wenda;
    //       var wenda = [];
    //       for (var i = 0; i < x.length; i++) {
    //         if (x[i].p_wenda_id == 0) {
    //           wenda.push(x[i])
    //         }
    //       }
    //       that.setData({
    //         wenda: wenda
    //       })
    //       console.log(arr)
    //     }
    //   )
    // },// 获取详情


    btnliked() {

        var that = this;
        var liked = that.data.liked;
        if (liked == false) {
            that.liked()
        } else {
            that.dislike()
        }
    },
    liked() {
        wx.showLoading({
            title: '加载中',
        })
        var opid = wx.getStorageSync('user').openid;
        var id = this.data.id;
        var liked = this.data.liked;

        app.core('get', 'create_user_collect/open_id/' + opid + '/type/1/lid/' + id + '', '', (res => {
            this.setData({
                liked: true,
            })
            console.log(this.data.liked)
            wx.showToast({
                title: '收藏成功',
                icon: 'none',
                duration: 1000
            })
            wx.hideLoading()
        }))


    },// 收藏
    dislike() {

        var that = this;
        var id = that.data.id;
        var like = that.data.liked;
        var opid = wx.getStorageSync('user').openid;

        app.core('get', 'cancel_user_collect/open_id/' + opid + '/type/2/lid/' + id + '', "", (res => {
            console.log(res)
            wx.showToast({
                title: '取消收藏',
                icon: 'none',
                duration: 1000
            })
            that.setData({
                liked: false
            })
        }))


    },// 取消收藏

    previewImage(e) {
        var current = e.target.dataset.src;
        var arr = []
        arr.push(current);
        wx.previewImage({
            current: current, // 当前显示图片的http链接
            urls: arr
        })

    },//预览图片
    addmore() {
        var show = this.data.showmore;

        if (show == true) {
            console.log(show)
            this.setData({
                showmore: false,
                moretext: '点击查看更多'
            })
        }
        else if (show == false) {
            console.log(show)
            this.setData({
                showmore: true,
                moretext: '点击收起'
            })
        }

    },
    // 点击加载更多
    share() {
        wx.updateShareMenu({
            withShareTicket: true,
            success(res) {
                console.log(res)
            }
        })
    },

    // 分享转发
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        return {
            title: this.data.detail.projectName,
            path: 'pages/loupan/detail?id=' + this.data.id + ''
        }
    },
    showModal(e) {

        let open_id = wx.getStorageSync('user_new');
        if  (open_id == undefined || open_id == '') {
            wx.showToast({
                title: '请先登录',
                duration: 2000,
                icon: 'none'
            })

            setTimeout(() => {
                wx.reLaunch({
                    url: '/pages/member/index',
                })

            }, 1000)

            return false;
        }

        var houseid = e.currentTarget.dataset.id;

        this.setData({
            modalName: e.currentTarget.dataset.target,
            houseid: houseid
        })


    },
    pickerchange(e) {
        console.log(e);
        var name = this.data.picker[e.detail.value]
        this.setData({
            value: name
        })
    },

    getname(e){
        this.setData({
            name: e.detail.value
        })

    },
    gettel(e){
        this.setData({
            tel: e.detail.value
        })

    },

    hideModal(e) {
        this.setData({
            modalName: null
        })
    },
    submitmodal() {
        var project_id = this.data.houseid;
        var that = this;
        var name = that.data.name;
        var phone = that.data.tel;
        var sex = that.data.value;
        if (name == '') {
            wx.showToast({
                title: '请输入姓名',
                duration: 1000,
                icon: 'none'
            });
            return;
        } else if (phone == '') {
            wx.showToast({
                title: '请输入手机号码',
                duration: 1000,
                icon: 'none'
            })
        } else {

            let open_id = wx.getStorageSync('user_new');
            if  (open_id == undefined || open_id == '') {
                wx.showToast({
                    title: '请先登录',
                    duration: 1000,
                    icon: 'none'
                })

                wx.reLaunch({
                    url: '/pages/member/index',
                })

                return false;
            }

            app.core('post', 'newhouse/order', {
                open_id: open_id,
                name: name,
                phone: phone,
                type: 2,
                project_id: project_id,
                sex: sex
            }, (res => {
                this.setData({
                    modalName: null
                })
                if (res.resultCode == 200) {

                    if (res.data && res.data.status == 0 && res.data.bargain_money > 0) {

                        wx.showToast({
                            title: '预定成功，需要支付订金' + res.data.bargain_money + '元',
                            duration: 1000,
                            icon: 'none'
                        })

                        app.core('post', 'newhouse/wxpay', { id: res.data.id }, (res => {
                            console.log(res)
                            wx.requestPayment({
                                'timeStamp': res.data.timeStamp,
                                'nonceStr': res.data.nonceStr,
                                'package': res.data.package,
                                'signType': res.data.signType,
                                'paySign': res.data.paySign,
                                'success':function(res){
                                    console.log(res)
                                    wx.showToast({
                                        title: '支付成功',
                                        duration: 1000,
                                        icon: 'none'
                                    })

                                    wx.navigateTo({
                                        url: '/pages/newhouse/myorder/index'
                                    })
                                },
                                'fail':function(res){
                                    console.log(res)
                                    wx.showToast({
                                        title: '支付失败',
                                        duration: 1000,
                                        icon: 'none'
                                    })

                                    wx.navigateTo({
                                        url: '/pages/newhouse/myorder/index'
                                    })
                                },
                                'complete':function(res){
                                    console.log(res)
                                }
                            })
                        }))
                    } else {

                        wx.showToast({
                            title: '预定成功',
                            duration: 1000,
                            icon: 'none'
                        })

                        wx.navigateTo({
                            url: '/pages/newhouse/myorder/index'
                        })
                    }

                } else if (res.resultCode == 401) {

                    wx.showToast({
                        title: '请先登录',
                        duration: 1000,
                        icon: 'none'
                    })

                    wx.reLaunch({
                        url: '/pages/member/index',
                    })
                } else {
                    wx.showToast({
                        title: res.msg ,
                        duration: 1000,
                        icon: 'none'
                    })
                }


            }))

        }
    },


    /**
     *   跳转楼盘详情
     */
    toHouseDetail(){
        var id = this.data.id;
        wx.navigateTo({
            url: './detail?id='+id,
        })
    }
})
